/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.chiefmedicalofficer.mcd.util

import com.hp.hpl.jena.vocabulary.RDFS
import com.hp.hpl.jena.vocabulary.DC

/**
 * Generates parameterized SPARQL queries.
 * @author twilson
 */
class SparqlGenerator {

    static String subPropertyOf = RDFS.subPropertyOf().getURI()
    static String label = RDFS.label().getURI()
    static String domain = RDFS.domain().getURI()
    static String title = DC.title().getURI()

    /**
     * For a given node, get the predicate and object of every triple
     * for which it is the subject. Include DC:title
     *
     */
    public static String getObjectProperties(String subjectUri) {
        return """
SELECT ?prop ?propTitle ?propLabel ?parentProp ?obj ?objTitle
WHERE {<${subjectUri}> ?prop ?obj .
    OPTIONAL {?prop <${title}> ?propTitle} .
    OPTIONAL {?prop <${label}> ?propLabel} .
    OPTIONAL (?prop <${subPropertyOf}> ?parentProp} .
    OPTIONAL {?obj <${title}> ?objTitle} }
        """
    }

    /**
     * For a given node, return the subject and predicate of every triple
     * for which it is the object. Include DC:title
     *
     */
    public static String getSubjectProperties(String objectUri) {
        return """
SELECT ?subj ?subjTitle ?prop ?propTitle
WHERE {?subj ?prop <${objectUri}>}
    [ {?subj <${title}> ?subjTitle} ]
    [ {?prop <${title}> ?propTitle} ]
        """
    }

    /**
     * Get assignable predicates for a given class. Include predicate label.
     * Also include parent node, so we can return this in a tree structure
     * of sub-properties. Include RDFS:label
     *
     **/
    public static String getPredicates(String classUri) {

        return """
SELECT ?prop ?label ?parent
WHERE {?prop <${domain}> <${classUri}>}
    [ {?prop <${label}> ?label} ]
    [ {?prop <${subPropertyOf}> ?parent} ]

        """
    }
}

